package com.jt.project;

import com.jt.datasource.JedisDataSource;
import redis.clients.jedis.Jedis;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class SSODemo01 {
    public static void main(String[] args) {
        String username="Tony";
        String password="123456";
        String token=doLogin(username,password);
        Object resource=doGetResource(token);
        System.out.println(resource);
    }

    private static Object doGetResource(String token) {
        if (token==null||"".equals(token))
            throw new IllegalArgumentException("请先登录");
        Jedis jedis = JedisDataSource.getConnection();
        Map<String, String> userMap = jedis.hgetAll(token);
        jedis.close();
        if (userMap==null||userMap.size()==0)
            throw new IllegalArgumentException("请先登录");
        String permissions = userMap.get("permissions");
        String[] split = permissions.split(",");
        List<String> plist = Arrays.asList(split);
        if (!plist.contains("sys:res:list"))
            throw new IllegalArgumentException("no permissions");
        return "your resource";
    }

    private static String doLogin(String username, String password) {
        if (username==null||"".equals(username))
            throw new IllegalArgumentException("username can not null");
        if (!"Tony".equals(username))
            throw new IllegalArgumentException("用户不存在");
        if (!"123456".equals(password))
            throw new IllegalArgumentException("密码不正确");
        Jedis jedis = JedisDataSource.getConnection();
        String token= UUID.randomUUID().toString();
        jedis.hset(token, "username", username);
        jedis.hset(token, "permissions","sys:res:list,sys:res:create");
        jedis.expire(token, 60);
        jedis.close();
        return token;
    }


}
